package com.kunbo.cn.app.controller;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import com.kunbo.cn.entity.system.user.User;
import com.kunbo.cn.service.system.user.UserService;
import com.kunbo.cn.util.encrypt.MD5Utils;
import com.kunbo.cn.util.retu.BuildBean;
import com.kunbo.cn.util.retu.CommonResult;
import com.kunbo.cn.util.retu.ResultBean;

@RestController
@RequestMapping("/userBoss")
public class UserBossApp {

	@Resource
	private UserService userService;

	@RequestMapping("/login")
	public  CommonResult login(HttpServletRequest request, User user, HttpSession session) {
		String name = request.getParameter("userName");
		String password = request.getParameter("password");
		user = userService.check(name, MD5Utils.md5(password));
		if (user == null) {
			return new CommonResult(false, "用户名或者密码错误", null);
		} else {
			session.setAttribute("userApp", user);
//			identityService.setAuthenticatedUserId(user.getActiviti_uuid());//设置activiti 用户登陆
//			Group group = identityService.createGroupQuery().groupMember(user.getActiviti_uuid()).singleResult();//查询用户组信息
//			session.setAttribute("group",group);
			return new CommonResult(true, "验证通过", user);
		}
	}
	
	
	/**
	 * 修改密码
	 */
	@PostMapping("/updatePassword")
	public ResultBean<Boolean> updatePassword(
			@RequestParam("id")Integer id,
			@RequestParam("password")String password
			){
		ResultBean<Boolean>  result = new ResultBean<Boolean>();
		return BuildBean.buildResult(result, "success", "error", userService.updatePassword(id,password));
	}
	
	/**
	 * 修改手机号
	 */
	@PostMapping("/updatePhone")
	public ResultBean<Boolean> updatePhone(
			@RequestParam("oldPhone")String oldPhone,
			@RequestParam("newPhone")String newPhone
			){
		ResultBean<Boolean> result = new ResultBean<Boolean>();
		return BuildBean.buildResult(result, "success", "error", userService.updatePhone(oldPhone,newPhone));
	}
	
	
	/**
	 * 忘记密码
	 */
	@PostMapping("/setPassword")
	public ResultBean<Boolean> setPassword(
			@RequestParam("phone")String phone,
			@RequestParam("password")String password
			){
		ResultBean<Boolean> result = new ResultBean<Boolean>();
		return BuildBean.buildResult(result, "success", "error", userService.setPasswordByPhone(phone,password));
	}
	
	
	
	

}
